package com.ghy.springbootsentinel;

import com.google.common.util.concurrent.RateLimiter;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author: 卢佳新
 * @version: v1.0
 * @description: com.ghy.springbootsentinel
 * @date:2022/1/3 9:17
 * @Motto 不积跬步，无以至千里；不积小流，无以成江海。
 */
//单机限流
public class RateLimiterDemo {
    //令牌桶算法
   private static RateLimiter rateLimiter=RateLimiter.create(10); //TPS=10

    public static void doRequest(){
        if(rateLimiter.tryAcquire()){ //获取令牌
            System.out.println("获取令牌成功");
        }else{
            System.out.println("获取令牌失败");
        }
    }

    public static void main(String[] args) {
        ExecutorService ex= Executors.newFixedThreadPool ( 5 );
        for (int i=0;i<5;i++){
            ex.submit ( new Runnable () {
                @Override
                public void run() {
                    for (int j=0;j<5;j++){
                        doRequest();
                    }
                }
            } );
        }
    }
}
